package com.systar.activity.parse;

import com.google.common.eventbus.EventBus;
import com.systar.activity.events.ParsingActivityEvent;
import com.systar.activity.state.FileActivity;

abstract class SessionFileParser extends FileParser
{

	public SessionFileParser(FileActivity activity, EventBus eventBus)
	{
		super(activity, eventBus);
	}

	protected boolean submit(String eventKind, String modulePriority, String message)
	{
		if ("Error".compareToIgnoreCase(eventKind) == 0)
		{
			if ("Critical".compareToIgnoreCase(modulePriority) == 0)
			{
				nbErr++; // only error on critical module is considered as real error
			}
			else
			{
				nbWarn++;
			}
		}
		else if ("Warning".compareToIgnoreCase(eventKind) == 0)
		{
			nbWarn++;
		}
		else
		{
			nbInfo++;
		}

		boolean submit = (nbErr > 0) || (nbInfo + nbWarn > SUBMIT_TRIGGER);
		if (submit)
		{
			eventBus.post(new ParsingActivityEvent(nbInfo, nbWarn, nbErr, (nbErr > 0) ? message : null));
			// clear counters
			nbInfo = nbWarn = nbErr = 0;
		}
		return submit;
	}

	protected boolean flush()
	{
		boolean submit = (nbInfo > 0) || (nbWarn > 0) || (nbErr > 0);
		if (submit)
		{
			eventBus.post(new ParsingActivityEvent(nbInfo, nbWarn, nbErr, null));
		}
		eventBus.post(activity);
		return submit;
	}

}
